cc.Class({
    extends: cc.Component,

    properties: {
       distance:0,
       target:{
           default:null,
           type:cc.Node
       }
    },
    onLoad:function(){
        this.setControlEvent();
    },
    setControlEvent:function(){
        var self=this;
        var bar=self.node.getChildByName("rockerbar");
        var touchCallBack=function(event){
            var target=event.getCurrentTarget();
            var touchPos=target.convertToNodeSpaceAR(event.touch.getLocation());
            var dis=self.getDistance(touchPos,cc.v2(0,0));
            if(dis>self.distance){
                dis=60;
                var x=dis*Math.cos(self.getRadian(touchPos));
                var y=dis*Math.sin(self.getRadian(touchPos));
                if(x>0){
                    self.target.getComponent("player").accLeft=false;
                    self.target.getComponent("player").accRight=true;
                }else if(x<0){
                    self.target.getComponent("player").accLeft=true;
                    self.target.getComponent("player").accRight=false; 
                }else{
                    self.target.getComponent("player").accLeft=false;
                    self.target.getComponent("player").accRight=false;
                }
                bar.setPosition(x,y);
            }else{
                bar.setPosition(touchPos);
            }
        };
        this.node.on(cc.Node.EventType.TOUCH_START,touchCallBack,this.node);
        this.node.on(cc.Node.EventType.TOUCH_MOVE,touchCallBack,this.node);
        this.node.on(cc.Node.EventType.TOUCH_END,function(event){
            bar.setPosition(0,0);
            self.target.getComponent("player").accLeft=false;
            self.target.getComponent("player").accRight=false;
        },this.node);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,function(event){
            bar.setPosition(0,0);
            self.target.getComponent("player").accLeft=false;
            self.target.getComponent("player").accRight=false;
        },this.node);
    },
    getRadian:function(point){
        var pos=cc.v2(0,0);
        var angle=Math.atan2(point.y-pos.y,point.x-pos.x);
        return angle;
    },
    getDistance:function(pos1,pos2){
        return Math.sqrt(Math.pow(pos1.x-pos2.x,2)+Math.pow(pos1.y-pos2.y,2));
    }
});
